package com.mallcloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.admin.api.entity.SysSocialDetails;
import com.mallcloud.mall.admin.service.SysSocialDetailsService;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.core.util.ValidGroup;
import com.mallcloud.mall.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "三方账号管理模块")
@RestController
@RequestMapping("/social")
@AllArgsConstructor
public class SysSocialDetailsController {
	private final SysSocialDetailsService sysSocialDetailsService;

	@ApiOperation("社交登录账户简单分页查询")
	@GetMapping("/page")
	public R getSocialDetailsPage(Page page, SysSocialDetails sysSocialDetails) {
		return R.ok(sysSocialDetailsService.page(page, Wrappers.query(sysSocialDetails)));
	}

	@ApiOperation("信息")
	@GetMapping("/{type}")
	public R getByType(@PathVariable("type") String type) {
		return R.ok(sysSocialDetailsService.list(Wrappers.<SysSocialDetails>lambdaQuery().eq(SysSocialDetails::getType, type)));
	}

	@ApiOperation("保存")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_social_details_add')")
	public R save(@Valid @RequestBody SysSocialDetails sysSocialDetails) {
		return R.ok(sysSocialDetailsService.save(sysSocialDetails));
	}

	@ApiOperation("修改")
	@PostMapping("/update")
	@PreAuthorize("@pms.hasPermission('sys_social_details_edit')")
	public R updateById(@Validated({ ValidGroup.Update.class }) @RequestBody SysSocialDetails sysSocialDetails) {
		sysSocialDetailsService.updateById(sysSocialDetails);
		return R.ok(Boolean.TRUE);
	}

	@ApiOperation("删除")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_social_details_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(sysSocialDetailsService.removeById(id));
	}

	@ApiOperation("通过社交账号、手机号查询用户、角色信息")
	@Inner
	@GetMapping("/info/{inStr}")
	public R getUserInfo(@PathVariable String inStr) {
		return R.ok(sysSocialDetailsService.getUserInfo(inStr));
	}

	@ApiOperation("绑定社交账号")
	@GetMapping("/bind")
	public R<Boolean> bindSocial(@RequestParam @ApiParam(name = "类型 MINI-微信小程序") String state, @RequestParam @ApiParam(name = "授权code") String code) {
		return R.ok(sysSocialDetailsService.bindSocial(state, code));
	}

	@ApiOperation("绑定手机号")
	@GetMapping("/bind-phone")
	public R<Boolean> bindPhone(@RequestParam(defaultValue = "MINI") @ApiParam(name = "类型 MINI-微信小程序") String state, @RequestParam @ApiParam(name = "手机授权code") String phoneCode) {
		return R.ok(sysSocialDetailsService.bindPhone(state, phoneCode));
	}

	@ApiOperation("绑定mall信息")
	@GetMapping("/bind-mall")
	public R<Boolean> bindMall(@RequestParam(defaultValue = "MINI") @ApiParam(name = "类型 MINI-微信小程序") String state) {
		return R.ok(sysSocialDetailsService.bindMall(state));
	}

	@ApiOperation("绑定手机号 mall信息")
	@GetMapping("/bind-phone-mall")
	public R<Boolean> bindPhoneMall(@RequestParam(defaultValue = "MINI") @ApiParam(name = "类型 MINI-微信小程序") String state, @RequestParam @ApiParam(name = "手机授权code") String phoneCode) {
		return R.ok(sysSocialDetailsService.bindPhoneMall(state, phoneCode));
	}

}
